﻿using SuperMap.Data;
using SuperMap.Desktop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SuperMap.HB.ThemeWork
{
    public class DesktopPlugin : Plugin
    {
        private readonly string m_PluginInfo;

        public DesktopPlugin(PluginInfo pluginInfo) : base(pluginInfo)
        {
            m_PluginInfo = pluginInfo.ToString();
        }

        public override bool Initialize()
        {
            bool bResult = false;

            try
            {
                WorkspaceConnectionInfo info = new WorkspaceConnectionInfo
                {
                    Type = WorkspaceType.SXWU,
                    Server = AppDomain.CurrentDomain.BaseDirectory + @"Data\Data.sxwu"
                };
                Application.ActiveApplication.Workspace.Open(info);

                Application.ActiveApplication.MainForm.ActiveFormChanged += MainForm_ActiveFormChanged;

                bResult = true;
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
                bResult = false;
            }

            return bResult;
        }

        private void MainForm_ActiveFormChanged(object sender, EventArgs e)
        {
            try
            {
                if (Application.ActiveForm is IFormMap)
                {
                    (Application.ActiveForm as IFormMap).MapControl.Map.Refresh();
                }
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        public override bool ExitInstance()
        {
            return base.ExitInstance();
        }

        public override string GetDefaultConfigString()
        {
            return base.GetDefaultConfigString();
        }
    }
}
